package com.quxiu.file.service;

import com.quxiu.file.constant.FileChannel;
import com.quxiu.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * 本地文件存储
 *
 * @author quxiu
 */
@Service
@ConditionalOnProperty(
        prefix = "file",
        name = {"channel"},
        havingValue = FileChannel.LOCAL,
        matchIfMissing = true
)
public class LocalSysFileServiceImpl implements ISysFileService {
    /**
     * 资源映射路径 前缀
     */
    @Value("${localFile.prefix}")
    public String localFilePrefix;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${localFile.path}")
    private String localFilePath;

    /**
     * 本地文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        return FileUploadUtils.upload(localFilePath, file);
    }

    @Override
    public InputStream downloadFile(String filePath) throws Exception {
        return FileUploadUtils.readFile(localFilePath + filePath);
    }
}
